import picturePuzzleAssembler from "./picturePuzzleAssembler";
import { state } from "./state";

const { ccclass, property, executeInEditMode } = cc._decorator;
const gfx = cc['gfx'];
@ccclass
@executeInEditMode
export default class picturePuzzleSprite extends cc.Sprite {

    _assembler: picturePuzzleAssembler = null;
    colNum: number = null;
    rowNum: number = null;
    picWidth: number = null;
    picHeight: number = null;
    top: state = null;
    bottom: state = null;
    left: state = null;
    right: state = null;
    posOffset: cc.Vec2 = null;
    status: Object = null;
    rootSize: cc.Size = null;
    isLoaded = false;
    onLoad() {
        window['picturePuzzleSprite'] = this;
    }
    onEnable() {
        super.onEnable();
    }
    // 生成对应形状
    genVerts(spriteFrame: cc.SpriteFrame,
        colNum: number, rowNum: number,
        picWidth: number, picHeight: number,
        status, posOffset: cc.Vec2,
        rootSize: cc.Size
    ) {
        let assembler = this._assembler;
        if (!assembler) {
            return;
        }
        // console.log('getVerts3');
        this.spriteFrame = spriteFrame;
        // spriteFrame.getTexture().setFilters(cc.Texture2D.Filter.LINEAR, cc.Texture2D.Filter.LINEAR)
        // console.error(new Error())
        // debugger
        assembler.colNum = this.colNum = colNum;
        assembler.rowNum = this.rowNum = rowNum;
        assembler.picWidth = this.picWidth = picWidth;
        assembler.picHeight = this.picHeight = picHeight;

        // assembler.isTopExtend = this.top = state.outter;
        // assembler.isBottomExtend = this.bottom = state.outter;
        // assembler.isLeftExtend = this.left = state.outter;
        // assembler.isRightExtend = this.right = state.outter;

        // assembler.isTopExtend = this.top = state.inner;
        // assembler.isBottomExtend = this.bottom = state.inner;
        // assembler.isLeftExtend = this.left = state.inner;
        // assembler.isRightExtend = this.right = state.inner;

        // assembler.isTopExtend = this.top = state.normal;
        // assembler.isBottomExtend = this.bottom = state.normal;
        // assembler.isLeftExtend = this.left = state.normal;
        // assembler.isRightExtend = this.right = state.normal;

        assembler.isTopExtend = this.top = status.top;
        assembler.isBottomExtend = this.bottom = status.bottom;
        assembler.isLeftExtend = this.left = status.left;
        assembler.isRightExtend = this.right = status.right;

        assembler.rootSize = this.rootSize = rootSize;
        this.node.setContentSize(this.picWidth, this.picHeight)
        assembler.posOffset = this.posOffset = posOffset;
        this.isLoaded = true;
        // console.log('getVerts3', assembler);
        // assembler.anchorPoint = cc.v2(0, 0);
        this.setVertsDirty();
        // assembler.updateRenderData(this);
    }


    _resetAssembler() {
        this.setVertsDirty();
        let assembler = this._assembler = new picturePuzzleAssembler();

        assembler.init(this);

        //@ts-ignore
        this._updateColor();
    }
}
